package com.photoeditor.demo.model.subscribe;

import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.common.base.utils.SystemUtils;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.util.TimeAutoCheckUtils;
import com.photoeditor.demo.util.VpnUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Locale;

import static com.photoeditor.demo.statistics.StatisticsConstant.STATISTIC_EVENT_LANGZH;
import static com.photoeditor.demo.statistics.StatisticsConstant.STATISTIC_EVENT_NOSIM;
import static com.photoeditor.demo.statistics.StatisticsConstant.STATISTIC_EVENT_ROOTPHONE;

/**
 * Created by @author winn on 2019/5/15.
 */
public class UserHelper {

    private ArrayList<WeakReference<OnUserTypeChange>> mListenerList = new ArrayList<>();

    private static class Holder {
        private static UserHelper sInstance = new UserHelper();
    }

    public static UserHelper getInstance() {
        return Holder.sInstance;
    }

    private UserHelper() {
//        CountryUtils.addCountryIpListener(new CountryUtils.CountryIpListener() {
//            @Override
//            public void onCountryChange() {
//                // ip为中国
//                String ipCountry = CountryUtils.getCountryByIp();
//                if (!TextUtils.isEmpty(ipCountry) && "cn".equals(ipCountry)) {
//                    StatisticsUtils.statisticsCustomFunction(STATISTIC_EVENT_CHINAIP);
//                }
//                notifyUserTypeChange();
//            }
//        });
//        BuyUserManager.getInstance().addBuyUserListener(new BuyUserManager.BuyUserListener() {
//            @Override
//            public void setBuyUser(String s, int i) {
//                notifyUserTypeChange();
//            }
//        });
    }

    private void notifyUserTypeChange() {
        for (WeakReference<OnUserTypeChange> listener : mListenerList) {
            if (listener != null && listener.get() != null) {
                listener.get().onUserTypeChanged();
            }
        }
    }

    /**
     * 添加用户买量类型变化监听，
     * 注意：不能使用局部临时变量
     * @param listener
     */
    public void addListener(OnUserTypeChange listener) {
        mListenerList.add(new WeakReference<OnUserTypeChange>(listener));
    }

    public void removeListener(OnUserTypeChange listener) {
        for (WeakReference<OnUserTypeChange> tmp : mListenerList) {
            if (tmp != null && tmp.get() != null) {
                if (listener == tmp.get()) {
                    mListenerList.remove(tmp);
                    return;
                }
            }
        }
    }

    public boolean isBuyUser() {
//        int userType = BuyUserManager.getInstance().getBuyUserType();
//        if (userType == BuyUserManager.BuyType.TYPE_ORGANIC) {
//            return false;
//        }
//        if (userType == BuyUserManager.BuyType.TYPE_UNKNOWN) {
////            String utmSourceReceiver = StrategySdk.getInstance(ComponentContext.getContext()).getUtmSourceReceiver();
////            /**
////             * google，google-play均为自然
////             */
////            if (TextUtils.equalsIgnoreCase("google", utmSourceReceiver) ||
////                    TextUtils.equalsIgnoreCase("google-play", utmSourceReceiver)) {
////                return false;
////            }
//            return false;
//        }
//        String country = Locale.getDefault().getCountry().toLowerCase();
//        // 本地语言为中文简体
//        if (!TextUtils.isEmpty(country) && "cn".equalsIgnoreCase(country)
//                && TestUser.getBooleanValue("s_s_country", true)) {
//            return false;
//        }
//        // ip为中国
//        String ipCountry = CountryUtils.getCountryByIp();
//        if (!TextUtils.isEmpty(ipCountry) && "cn".equalsIgnoreCase(ipCountry)
//                && TestUser.getBooleanValue("s_s_ip_c", true)) {
//            return false;
//        }
//        // 没sim卡
//        boolean noSim = TextUtils.isEmpty(SystemUtils.getSimCountry(ComponentContext.getContext()));
//        if (noSim && TestUser.getBooleanValue("s_s_sim_phone", true)) {
//            return false;
//        }
//        // root手机
//        if (DeviceUtils.isRoot() && TestUser.getBooleanValue("s_s_root", true)) {
//            return false;
//        }
//        // sim卡为中国
//        String simCountry = CountryUtils.getCountryBySim();
//        if (!TextUtils.isEmpty(simCountry) && "cn".equalsIgnoreCase(simCountry)
//                && TestUser.getBooleanValue("s_s_sim_c", true)) {
//            return false;
//        }
//        return true;
        return false;
    }

    /**
     * 统计规避策略相关埋点
     */
    public void statisticsPhoneState() {
        // 使用VPN，内部做逻辑判断后，如果使用VPN，直接上传统计
        VpnUtils.getInstance().init();
        // 没sim卡
        boolean noSim = TextUtils.isEmpty(SystemUtils.getSimCountry(ComponentContext.getContext()));
        if (noSim) {
            StatisticsUtils.statisticsCustomFunction(STATISTIC_EVENT_NOSIM);
        }
        // root手机
        if (DeviceUtils.isRoot()) {
            StatisticsUtils.statisticsCustomFunction(STATISTIC_EVENT_ROOTPHONE);
        }
        String country = Locale.getDefault().getCountry().toLowerCase();
        // 本地语言为中文简体
        if (!TextUtils.isEmpty(country) && "cn".equalsIgnoreCase(country)) {
            StatisticsUtils.statisticsCustomFunction(STATISTIC_EVENT_LANGZH);
        }
        // ip为中国
//        String ipCountry = CountryUtils.getCountryByIp();
//        if (!TextUtils.isEmpty(ipCountry) && "cn".equalsIgnoreCase(ipCountry)) {
//            StatisticsUtils.statisticsCustomFunction(STATISTIC_EVENT_CHINAIP);
//        }
//        // sim卡为中国
//        String simCountry = CountryUtils.getCountryBySim();
//        if (!TextUtils.isEmpty(simCountry) && "cn".equalsIgnoreCase(simCountry)) {
//            StatisticsUtils.statisticsCustomFunction(STATISTIC_EVENT_CHINASIM);
//        }
        // 修改时间，内部做逻辑判断后，如果修改了时间，直接上传统计
        TimeAutoCheckUtils.getInstance().setUp();
    }


    public interface OnUserTypeChange {
        void onUserTypeChanged();
    }
}
